/**
 * @author Michael,Aug 20, 2012
 *
 */
package com.util;

import java.io.IOException;
import java.util.Properties;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * @author Michael
 *
 */
public class EncodeFilter implements Filter {
	 private String encoding="UTF-8";
	 private boolean forceEncoding = true; 

	@Override
	public void destroy() {
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		Properties prop = System.getProperties();
		prop.put("file.encoding", "UTF-8"); 
		  if (this.encoding != null && (this.forceEncoding || request.getCharacterEncoding() == null)) {
	            request.setCharacterEncoding(this.encoding);
	            if (this.forceEncoding) {
	                response.setContentType("text/html; charset=" + this.encoding);
	            }
	        } 
		chain.doFilter(request, response);
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
	}

}
